package com.ybt.router.tcp.routertable.manager;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.ybt.configration.IYBTConfigration;
import com.ybt.configration.YBTConfigration;
import com.ybt.router.core.message.factory.IMessageFactory;
import com.ybt.router.core.receiver.factory.IReceiverFactory;
import com.ybt.router.core.routertable.IServiceRouter;
import com.ybt.router.core.routertable.manager.RouterContainer;
import com.ybt.router.core.sender.factory.ISenderFactory;
import com.ybt.router.tcp.routertable.TCPAddress;
import com.ybt.router.tcp.routertable.TCPServiceRouter;

public class TCPRouterManager {
    private static volatile TCPRouterManager factory;
    private static String CONFIG_DIR = "router"+File.separator+"serviceRouter.xml";
    private static Logger logger = Logger.getLogger(TCPRouterManager.class);
    private RouterContainer container;
    private TCPRouterManager() {
        CONFIG_DIR = YBTConfigration.getInstance().getProperty(
                IYBTConfigration.WORKFLOW_DEPLOY_PATH) + File.separator + CONFIG_DIR;
        container = RouterContainer.getInstance();
        loadFromLocal();
    }

    public static TCPRouterManager getInstance() {
        if (factory == null) {
            synchronized(TCPRouterManager.class){
                if (factory == null) {
                    factory = new TCPRouterManager();
                }
            }
        }
        return factory;
    }
    public TCPServiceRouter getRouter(String serviceName){
        IServiceRouter temp = container.getRouter(serviceName);
        return temp==null?null:(TCPServiceRouter)temp;
    }
    private void loadFromLocal() {
        File dir = new File(CONFIG_DIR);
        Document doc = null;
        if (dir.exists()) {
            // 配置文件已经存在
            FileInputStream fi = null;
            try {
                fi = new FileInputStream(dir);
                SAXReader saxReader = new SAXReader();
                doc = saxReader.read(new InputStreamReader(fi, "utf8"));
            } catch (Exception e) {
                logger.error("loading " + CONFIG_DIR + " error ", e);
                return;
            } finally {
                if (fi != null) {
                    try {
                        fi.close();
                    } catch (Exception e) {
                        logger.error("closeing " + CONFIG_DIR + " error ",
                                e);
                    }
                }
            }
        } else {
            // 配置文件没存在
            logger.error("the service router file " + CONFIG_DIR + " don't exist ");
            return;
        }
        
        List<Element> eles = doc.getRootElement().elements();
        if (eles == null)
            return;

        // 服务路由配置
        for (Element conEle : eles) {
            IServiceRouter router = new TCPServiceRouter();
            //服务名
            String name = conEle.attributeValue("name");
            router.setServiceName(name);
            
            //接入地址
            String destination = conEle.element("inAddress").getText();
            TCPAddress inAdd = new TCPAddress();
            inAdd.setDestination(destination);
            router.setInAddress(inAdd);

            //接出地址
            destination = conEle.element("outAddress").getText();
            TCPAddress outAdd = new TCPAddress();
            outAdd.setDestination(destination);
            router.setOutAddress(outAdd);
            if(logger.isDebugEnabled()){
                logger.debug("parse the service router " + router);
            }
            container.updateRouter(router);
        }

    }

    class RouterType{
        String type;
        IMessageFactory msgFactory;
        IReceiverFactory receFactory;
        ISenderFactory sendFactory;
    }
    
    public static void main(String[] args) {
        TCPRouterManager.getInstance();
    }
}
